package com.test.web3j;

import org.web3j.protocol.Web3j;
import org.web3j.protocol.admin.Admin;
import org.web3j.protocol.http.HttpService;

/**
 * web3j客户端（饿汉式单例）
 */
public class Web3JClient {
	
	private static String url = "http://127.0.0.1:8545";
//	private static String url = "https://mainnet.infura.io/v3/09ade678f2de415eab21fdb8f8c26328";
//	private static String url = "https://kovan.infura.io/yXDUNwlNOcx0UJCWjzNr";

    private Web3JClient(){}

    private volatile static Web3j web3j;
    private volatile static Admin admin;

    public static Web3j getWeb3j(){
        if(web3j==null){
            synchronized (Web3JClient.class){
                if(web3j==null){
                    web3j = Web3j.build(new HttpService(url));
                }
            }
        }
        return web3j;
    }

    public static Admin getAdmin(){
        if(admin==null){
            synchronized (Web3JClient.class){
                if(admin==null){
                	admin = Admin.build(new HttpService(url));
                }
            }
        }
        return admin;
    }
}
